package com.ajk.message.util;

import java.io.IOException;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;


public class HttpClientUtil {
	
	public static String doGet(String url,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpGet httpGet = null;
		HttpResponse response = null;
		try {
			client = HttpClientBuilder.create().build();
			httpGet = new HttpGet(url);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpGet.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpGet);
			HttpEntity entity = response.getEntity();
			resData = EntityUtils.toString(entity);
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}
	
	public static String doPost(String url,String body,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpPost httpPost = null;
		HttpResponse response = null;
		if(body==null){
			body = "";
		}
		try {
			client = HttpClientBuilder.create().build();
			httpPost = new HttpPost(url);
			StringEntity entity = new StringEntity(body, "UTF-8");//解决中文乱码问题    
			httpPost.setEntity(entity);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpPost.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpPost);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}

	public static String doPut(String url,String body,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpPut httpPut = null;
		HttpResponse response = null;
		if(body==null){
			body = "";
		}
		try {
			client = HttpClientBuilder.create().build();
			httpPut = new HttpPut(url);
			StringEntity entity = new StringEntity(body, "UTF-8");//解决中文乱码问题    
			httpPut.setEntity(entity);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpPut.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpPut);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}
	
	public static String doDelete(String url,String ...headers){
		String resData = null;
		CloseableHttpClient client = null;
		HttpDelete httpDelete = null;
		HttpResponse response = null;
		try {
			client = HttpClientBuilder.create().build();
			httpDelete = new HttpDelete(url);
			if(ArrayUtils.isNotEmpty(headers) && headers.length % 2==0){
				for(int i=0; i<headers.length; i=i+2){
					httpDelete.addHeader(headers[i],headers[i+1]);
				}
			}
			response = client.execute(httpDelete);
			HttpEntity resEntity = response.getEntity();
			resData = EntityUtils.toString(resEntity,"UTF-8");
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}finally{
			HttpClientUtils.closeQuietly(response);
			HttpClientUtils.closeQuietly(client);
		}
		return resData;
	}
	
}
